package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 桑基图
 *
 * @author chart
 */
@RestController
@RequestMapping("/sankey/chart")
public class SankeyChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;




    @PostMapping("/importData1")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }


            //数据的种类不能重复
            Set<String> xlist = new HashSet<>();
            List<List<Object>> dataList = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<Object> newList =new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            if (i1 == 0 || i1 == 1) {
                                xlist.add(s);
                            }
                            newList.add(s);
                        }
                    }
                    dataList.add(newList);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            List<Map> maps = new ArrayList<>();
            JSONObject series = (JSONObject) jsonObject.get("series");

            Iterator<String> iterator = xlist.iterator();
            while (iterator.hasNext()) {
                Map map = new HashMap<>();
                map.put("name",iterator.next());
                maps.add(map);
            }

            series.put("data",maps);

            List<JSONObject> links = (List<JSONObject>) series.get("links");
            JSONObject jsonObject1 = links.get(0);

            List<JSONObject> linkList =new ArrayList<>();
            for (int i = 0; i < dataList.size(); i++) {
                List<Object> list = dataList.get(i);

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);

                jsonObject2.put("source",list.get(0));
                jsonObject2.put("target",list.get(1));
                jsonObject2.put("value",list.get(2));

                linkList.add(jsonObject2);
            }
            series.put("links",linkList);

            jsonObject.put("series",series);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

}
